/**
 * @author xiaobyu
 * @created Jun 16, 2016
 * 
 */
package mdt.figure;

import org.tigris.gef.base.Selection;
import org.tigris.gef.presentation.Fig;
import org.tigris.gef.presentation.FigEdge;
import org.tigris.gef.presentation.FigLine;
import org.tigris.gef.presentation.FigRect;
import mdt.model.INetworkDevice;
import mdt.model.INodeModel;

import java.awt.*;

public class FigNetworkDevice extends FigNodeEditable{

	private FigRect boundryFig;
	private FigLine lifeLine;

	public static final long serialVersionUID = 1L;

	public FigNetworkDevice(INetworkDevice owner) {
		super(owner, TextType.SINGLE_LINE);

		int textWidth = figText.getWidth();
		int textHeight = figText.getHeight();
		boundryFig = new FigNetworkDevicePort(0, 0, textWidth, 440);
		boundryFig.setVisible(false);

		lifeLine = new FigLine(textWidth / 2, textHeight, textWidth / 2, 420);
		lifeLine.setLineWidth(1);

		addFig(boundryFig);
		addFig(lifeLine);
		setShadowSize(1);
		setOwner(owner);
	}

	public void setOwner(Object owner) {
		super.setOwner(owner);
		if (boundryFig != null)
			this.bindPort(owner, boundryFig);
	}

	public void setDashed(boolean dashed) {
		super.setDashed(dashed);
		lifeLine.setDashed(dashed);
	}

	public FigNodeEditable cloneFig() {
		FigNetworkDevice cloned = (FigNetworkDevice)super.cloneFig();
		java.util.List<Fig> figs =  getFigs();
		int idx = figs.indexOf(boundryFig);
		cloned.boundryFig = (FigRect)cloned.getFigAt(idx);
		idx = figs.indexOf(lifeLine);
		cloned.lifeLine = (FigLine)cloned.getFigAt(idx);
		return cloned;
	}

	public boolean isDragConnectable() {
		return false;
	}

	protected void setBoundsImpl(int x, int y, int w, int h) {
		Rectangle oldBounds = getBounds();
		Dimension minSize = getMinimumSize();
		w = Math.max(w, minSize.width);
		h = Math.max(h, minSize.height);
		boundryFig.setBounds(x, y, w, h);
		int textFigHeight = figText.getHeight();
		figText.setBounds(x, y, w, textFigHeight);
		lifeLine.setBounds(x + w / 2, y + textFigHeight, w, h - textFigHeight);
		calcBounds(); // _x = x; _y = y; _w = w; _h = h;
		firePropChange("bounds", oldBounds, getBounds());
		updateEdges();
	}

	public Selection makeSelection() {
		return new SelectionNetworkDevice(this);
	}

	protected void initTextFig(INodeModel model) {
		super.initTextFig(model);
		figText.setLineWidth(1);
		figText.setTopMargin(5);
		figText.setBotMargin(5);
		figText.setLeftMargin(5);
		figText.setRightMargin(5);
	}


	class FigNetworkDevicePort extends FigRect {

		FigNetworkDevicePort(int x, int y, int width, int height) {
			super(x, y, width, height, null, null);
			setLineWidth(0);
		}

		@Override
		public Point getClosestPoint(Point anotherPt) {
			int width = 1;
			int y = anotherPt.y;

			int x = getX() + getWidth() / 2;
			// if the y coordinate is before the classifier role y,
			// we place the start in the corner of the fighead.
			if (y < lifeLine.getY() + 2) {
				y = getY() + figText.getHeight() + 2;
			}
			// else if the y coordinate is outside of the classifier role,
			// we fix the y in the max y of the classifier role.
			// FIXME: We should increase the height of the classifier role,
			// don't???
			else if (y > getY() + getHeight()) {
				y = figText.getY() + FigNetworkDevice.this.getHeight();
			}
			return new Point(x, y);
		}
	}

	public void translateCloned(int dx, int dy) {
		super.translate(dx, 0);
	}

	public Dimension getMinimumSize() {
		Rectangle rect = figText.getBounds();
		int textMinH = figText.getMinimumHeight();
		//TODO need to consider the alignment with other nodes
		for (FigEdge edge : getFigEdges()) {
			rect.add(edge.getBounds());
		}
		if (rect.height - textMinH < 50) {
			rect.height = textMinH + 50;
		}
		return new Dimension(figText.getMinimumSize().width, rect.height);
	}

	public Dimension reviseDragCoord(int dx, int dy) {
		return new Dimension(dx, 0);
	}


	public String toString() {
		return "Figure: Device " + ((INodeModel)getOwner()).getName();
	}
}
